package com.liangdemo.web.redis.utils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.liangdemo.web.Service.UserService;
import com.liangdemo.web.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;


/**
 * redis操作
 *
 * @author Mr.Liang
 * @date 2022/5/19 16:35
 */
@RestController
@RequestMapping("/redis")
public class redis {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private UserService userService;

    /**
     * 根据key获取数据
     *
     * @param key
     * @return
     */
    @GetMapping("/get/{key}")
    public Object get(@PathVariable("key") String key) {
        return redisTemplate.opsForValue().get(key);
    }

    /**
     * 保存数据
     *
     * @param key
     * @param value
     * @return
     */
    @PostMapping("/set/{key}/{value}")
    public Object set(@PathVariable("key") String key, @PathVariable("value") String value) {
        redisTemplate.opsForValue().set(key, value);


        return "set suess ";
    }

    @PostMapping("/folder")
    public void folder() {
        redisTemplate.opsForValue().set("String：1", "ceshi");
    }

    class Ceshi {
        private String id;
        private String name;

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }

    @PostMapping("/setKey")
    public String setKey(String key) {
        //  redisTemplate.boundSetOps("ceshi:" + key).add("张三");


        redisTemplate.opsForValue().set(key, "测试");

        return "";
    }

    /**
     * 获取指定文件夹下指定的key
     * @param key
     */
    @GetMapping("/getKey")
    public void getKey(String key) {
        Set<String> set = redisTemplate.keys("ceshi:*");
        System.out.println(set);
        set.forEach(e -> {
            System.out.println(e);
            System.out.println(redisTemplate.opsForValue().get(e));
        });

    }




    /**
     * 使用注解实现缓存
     * 先去缓存中查看是否有数据的存储在，如果有就直接返回
     * 如果没有就调用数据库去查询
     */
    @GetMapping("/getCache")
    @Cacheable(value = "db1", key = "#user.id")
    public List<User> getCache(User user) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getId, user.getId());
        return userService.list(wrapper);
    }

    /**
     * 将数据的返回值添加到缓存中
     *
     * @param user
     * @return
     */
    @GetMapping("/getCache1")
    @Cacheable(value = "User", key = "#user.id")
    public List<User> getCache1(User user) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getId, user.getId());
        List<User> userList = userService.list(wrapper);
        return userList;
    }

    @GetMapping("/getCache2")
    @CacheEvict(value = "User", key = "#user.id", condition = "1==1")
    public void test(User user) {
        System.out.println("方法被执行");
    }
}
